//路由鉴权：就是路由能不能被访问到权限设置->基于全局守卫实现
//引入路由器
import router from '@/router';
//引入进度条
import NProgress from 'nprogress';
//引入进度条样式
import 'nprogress/nprogress.css';
//引入用户相关的仓库
import useUserStore from '@/store/modules/user';
//引入大仓库
import pinia from '@/store'
let userStore = useUserStore(pinia);
//关闭进度条的加载小圆球
NProgress.configure({ showSpinner: false });

//存储用户未登录可以访问的路由路径
let whiteList = ['/home', '/hospital/register', '/hospital/detail', '/hospital/notice', '/hospital/close', '/hospital/search'];

//添加相应的全局守卫
//前置守卫
router.beforeEach((to, from, next) => {
    //访问路由组件的之前，进度条开始
    NProgress.start();
    //动态设置网页左上角的标题
    document.title = `尚医通-${to.meta.title}`

    //判断用户是否登录-token
    let token = userStore.userInfo.token;
    if (token) {
        //如果用户登录了，放行
        next();
    } else {
        //如果用户没有登录，判断是否要跳转到登录页面
        if (whiteList.includes(to.path)) {
            //放行
            next();
        } else {
            //登录组件显示出来
            userStore.visiable = true;
            next({ path: '/home', query: { redirect: to.fullPath } });
        }
    }
});

//后置守卫
router.afterEach((to, from) => {
    //访问路由组件之后，进度条结束
    NProgress.done();
});